<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class m_izin extends CI_Model {

    var $izin_id;
    var $izin_keluar;
    var $izin_masuk;
    var $izin_santri;
    var $izin_keperluan;
    var $izin_oleh;
    var $izin_dirubah;
    var $cari;
    var $paginate;
    var $num_pages;
    var $offset;

    function __construct() {
        parent::__construct();
        $this->load->library('auth');
        $this->load->helper('date');
        $this->load->model('m_santri');
    }

    public function get_row_all() {
        if ($this->cari) {
            $this->db->where('(izin_keperluan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        $this->db->order_by('izin_id', 'DESC');
        $query = $this->db->count_all_results('t_izin');

        return $query;
    }

    public function get() {
        if ($this->cari) {
            $this->db->where('(izin_keperluan LIKE ' . $this->db->escape('%' . $this->cari . '%') . ')');
        }

        if ($this->paginate == TRUE) {
            $this->db->order_by('izin_id', 'DESC');
            $query = $this->db->get('t_izin', $this->num_pages, $this->offset);
            return $query;
        }

        $this->db->order_by('izin_id', 'DESC');
        $query = $this->db->get('t_izin');
        return $query;
    }

    public function get_by_id($id = '0') {
        $this->db->where('izin_id', $id);
        $query = $this->db->get('t_izin');

        if ($query->num_rows() > 0) {
            return $query->row();
        } else {
            return redirect('homes/');
        }
    }

    function get_izin_by_santri_id($id = '0') {
        $this->db->where('izin_santri', $id);
        $this->db->order_by('izin_id', 'DESC');
        $query = $this->db->get('t_izin');

        $hasil = array();
        if ($query->num_rows() > 0) {
            //$hasil = $query->result_array();

            $no = 1;
            // Loop through all rows
            foreach ($query->result() as $row) {
                $hasil[] = array(
                    'izin_id' => $no,
                    'izin_santri' => $this->m_santri->get_by_id($row->izin_santri)->santri_nama,
                    'izin_keluar' => $row->izin_keluar,
                    'izin_masuk' => $row->izin_masuk,
                    'izin_keperluan' => $row->izin_keperluan,
                    'izin_oleh' => $row->izin_oleh,
                );
                $no++;
            }
        }

        return $hasil;
    }

    function get_izin_by_santri_tgl($mulai, $selesai) {
        //echo "tgl selesai" . $selesai . " <br />";
        $this->izin_keluar = date('Y/m/d', strtotime($mulai));
        $this->izin_masuk = date('Y/m/d', strtotime($selesai));
        //echo $this->izin_keluar . " - " . $this->izin_masuk . "<br />";
        //$this->db->where('izin_keluar >= "' . $this->izin_keluar . '" and izin_masuk <= "' . $this->izin_masuk . '"');

        $this->db->where('izin_masuk >= "' . $this->izin_masuk . '" and izin_keluar <= "' . $this->izin_keluar . '"');
        $this->db->order_by('izin_id', 'DESC');
        $query = $this->db->get('t_izin');
        //echo $this->izin_keluar . " - " . $this->izin_masuk;

        $hasil = array();
        if ($query->num_rows() > 0) {
            //$hasil = $query->result_array();

            $no = 1;
            // Loop through all rows
            foreach ($query->result() as $row) {
                $hasil[] = array(
                    'izin_id' => $no,
                    // 'izin_id' => $row->izin_id,
                    'izin_santri' => $this->m_santri->get_by_id($row->izin_santri)->santri_nama,
                    'izin_keluar' => $row->izin_keluar,
                    'izin_masuk' => $row->izin_masuk,
                    'izin_keperluan' => $row->izin_keperluan,
                    'izin_oleh' => $row->izin_oleh,
                        //'izin_dirubah' => $row->izin_dirubah
                );
                $no++;
            }
        }
        return $hasil;
    }

    public function save() {
        if ($this->izin_keperluan) {
            $db_array['izin_keperluan'] = $this->izin_keperluan;
        }

        if ($this->izin_santri) {
            $db_array['izin_santri'] = $this->izin_santri;
        }

        if ($this->izin_keluar) {
            $db_array['izin_keluar'] = $this->izin_keluar;
        }

        if ($this->izin_masuk) {
            $db_array['izin_masuk'] = $this->izin_masuk;
        }


        if ($this->izin_oleh) {
            $db_array['izin_oleh'] = $this->izin_oleh;
        }

        if ($this->izin_dirubah) {
            $db_array['izin_dirubah'] = $this->izin_dirubah;
        }

        if ($this->izin_id) {
            $this->db->where('izin_id', $this->izin_id);
            $this->db->update('t_izin', $db_array);
        } else {
            $this->db->insert('t_izin', $db_array);
        }
    }

    public function delete() {
        $this->db->where('izin_id', $this->izin_id);
        $this->db->delete('t_izin');
    }

}

/* End of file m_izin.php */
/* Location: ./application/model/m_izin.php */